#include "loop_module/loop.h"
#include "socket_address.h"
#include "socket_connection.h"
#include "tcp_socket_connection.h"

#include <cstdint>
#include <cstdio>
#include <gtest/gtest.h>
#include <memory>
#include <vector>

TEST(SocketConnection, AsyncConnect)
{
    Loop                              loop;
    std::shared_ptr<SocketConnection> connection = std::make_shared<TcpSocketConnection>(&loop);

    std::string                       domain     = "www.baidu.com";
    std::string                       protocol   = "http";
    std::vector<SocketAddress>        socket_address;
    SocketAddress::Address(domain, protocol, socket_address);
    SocketAddress address = *socket_address.data();

    connection->OpenSocket();
    connection->AsyncConnect(address, [&connection](std::shared_ptr<SocketConnection>, int result) {
        printf("connect result : %d.\n", result);
        std::string content = "GET / HTTP/1.1\r\nHost: www.baidu.com\r\n\r\n";
        connection->AsyncWrite((const uint8_t *)content.c_str(), content.size(),
                               [](std::shared_ptr<SocketConnection> c, int result, int size) {
                                   printf("write result %d and size : %d.\n", result, size);
                               });
        connection->AsyncRead([](std::shared_ptr<SocketConnection> c, int result, SocketConnection::Message & msg) {
            if (result >= 0)
            {
                printf("%s", std::string((char *)msg.m_data, msg.m_data_length).c_str());
                msg.m_use_length = msg.m_data_length;
            }
        });
    });
    loop.Run();
}

TEST(SocketConnection, Connection)
{

}
